import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";

var _dec, _dec2, _class;

import React from 'react';
import { Form, Modal, Icon, Select, Popconfirm, Input } from 'antd';
import intl from 'utils/intl';
import { getEditTableData } from 'utils/utils';
import EditTable from 'components/EditTable';
import { Bind } from 'lodash-decorators';
import styles from "./index.less?modules";
var FormItem = Form.Item;
var Option = Select.Option;
var ConditionModal = (_dec = Bind(), _dec2 = Bind(), (_class = /*#__PURE__*/function (_React$Component) {
  _inherits(ConditionModal, _React$Component);

  var _super = _createSuper(ConditionModal);

  function ConditionModal(props) {
    var _this;

    _classCallCheck(this, ConditionModal);

    _this = _super.call(this, props);
    var _props$paramList = props.paramList,
        paramList = _props$paramList === void 0 ? [] : _props$paramList;
    _this.state = {
      // loading: false,
      paramList: paramList.map(function (i) {
        return _objectSpread(_objectSpread({}, i), {}, {
          _status: 'update'
        });
      })
    };
    return _this;
  }

  _createClass(ConditionModal, [{
    key: "onDelete",
    value: function onDelete(deleteId) {
      var paramList = this.state.paramList;
      this.setState({
        paramList: paramList.filter(function (_ref) {
          var _status = _ref._status,
              _id = _ref._id,
              paramId = _ref.paramId;
          return _status === 'create' ? deleteId !== _id : deleteId !== paramId;
        })
      });
    }
  }, {
    key: "addParam",
    value: function addParam() {
      var paramList = this.state.paramList;
      paramList.push({
        _status: 'create',
        _id: paramList.length + 1
      });
      this.setState({
        paramList: paramList
      });
    }
  }, {
    key: "onOk",
    value: function onOk() {
      var paramList = this.state.paramList;
      var _this$props = this.props,
          onSave = _this$props.onSave,
          onClose = _this$props.onClose,
          readOnly = _this$props.readOnly;

      if (!readOnly) {
        var newParams = getEditTableData(paramList, ['_id', '_status']) || [];

        if (paramList.length > 0 && newParams.length === 0) {
          return;
        } // eslint-disable-next-line no-unused-expressions


        typeof onSave === 'function' && onSave(newParams);
      }

      onClose();
    }
  }, {
    key: "getParamValueByType",
    value: function getParamValueByType(type, form) {
      var readOnly = this.props.readOnly;

      if (type === 'fixed') {
        return /*#__PURE__*/React.createElement(Input, {
          trim: true,
          disabled: readOnly
        });
      }

      if (type === 'context') {
        return /*#__PURE__*/React.createElement(Select, {
          style: {
            width: '100%'
          },
          onChange: function onChange(v) {
            return form.setFieldsValue({
              paramKey: v
            });
          },
          dropdownClassName: styles['param-dropdown'],
          optionLabelProp: "title",
          disabled: readOnly
        }, /*#__PURE__*/React.createElement(Option, {
          value: "organizationId",
          title: intl.get('cpfm.customize.common.organizationId').d('采购方租户')
        }, /*#__PURE__*/React.createElement("div", {
          className: "option-title"
        }, intl.get('cpfm.customize.common.organizationId').d('采购方租户')), /*#__PURE__*/React.createElement("div", {
          className: "option-value"
        }, "organizationId")), /*#__PURE__*/React.createElement(Option, {
          value: "tenantId",
          title: intl.get('cpfm.customize.common.tenantId').d('供应商租户')
        }, /*#__PURE__*/React.createElement("div", {
          className: "option-title"
        }, intl.get('cpfm.customize.common.tenantId').d('供应商租户')), /*#__PURE__*/React.createElement("div", {
          className: "option-value"
        }, "tenantId")));
      }

      return /*#__PURE__*/React.createElement("span", null);
    }
  }, {
    key: "autoCompleteFieldKey",
    value: function autoCompleteFieldKey(id, form) {
      var _this$props$fieldList = this.props.fieldList,
          fieldList = _this$props$fieldList === void 0 ? {} : _this$props$fieldList;
      var fieldObj = (fieldList[form.getFieldValue("paramUnitId")] || []).find(function (i) {
        return i.modelFieldId === id;
      });
      form.setFieldsValue({
        paramKey: fieldObj.unitFieldCode
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var paramList = this.state.paramList;
      var _this$props2 = this.props,
          visible = _this$props2.visible,
          onClose = _this$props2.onClose,
          readOnly = _this$props2.readOnly,
          _this$props2$unitList = _this$props2.unitList,
          unitList = _this$props2$unitList === void 0 ? [] : _this$props2$unitList,
          _this$props2$fieldLis = _this$props2.fieldList,
          fieldList = _this$props2$fieldLis === void 0 ? {} : _this$props2$fieldLis,
          id = _this$props2.id;
      var columns = [{
        title: intl.get('cpfm.customize.common.paramKey').d('参数名'),
        dataIndex: 'paramKey',
        width: 210,
        render: function render(val, record) {
          if (['create', 'update'].includes(record._status)) {
            var $form = record.$form;
            return /*#__PURE__*/React.createElement(FormItem, null, $form.getFieldDecorator('paramKey', {
              initialValue: val,
              rules: [{
                required: !readOnly && true,
                message: intl.get('cpaas.common.validation.notNull', {
                  name: intl.get('cpfm.individual.common.paramKey').d('参数名')
                })
              }]
            })( /*#__PURE__*/React.createElement(Input, {
              trim: true,
              disabled: readOnly
            })));
          }

          return val;
        }
      }, {
        title: intl.get('cpfm.customize.common.paramType').d('参数类型'),
        dataIndex: 'paramType',
        width: 210,
        render: function render(val, record) {
          if (['create', 'update'].includes(record._status)) {
            var $form = record.$form;
            return /*#__PURE__*/React.createElement(FormItem, {
              wrapperCol: {
                span: 24
              }
            }, $form.getFieldDecorator('paramType', {
              initialValue: record.paramType || 'fixed'
            })( /*#__PURE__*/React.createElement(Select, {
              style: {
                width: '100%'
              },
              disabled: readOnly
            }, /*#__PURE__*/React.createElement(Option, {
              value: "context"
            }, intl.get('cpfm.customize.common.contextParam').d('上下文参数')), /*#__PURE__*/React.createElement(Option, {
              value: "fixed"
            }, intl.get('cpfm.customize.common.fixed').d('固定值')), /*#__PURE__*/React.createElement(Option, {
              value: "unit"
            }, intl.get('cpfm.customize.common.unit').d('表单字段')))));
          }

          return val;
        }
      }, {
        title: intl.get('cpfm.customize.common.paramValue').d('参数值'),
        dataIndex: 'paramValue',
        width: 420,
        render: function render(val, record) {
          if (['create', 'update'].includes(record._status)) {
            var $form = record.$form;
            var paramType = $form.getFieldValue('paramType');
            return /*#__PURE__*/React.createElement("div", {
              style: {
                width: '100%',
                display: 'flex'
              }
            }, /*#__PURE__*/React.createElement(FormItem, {
              style: {
                display: paramType === 'unit' ? 'inline-block' : 'none'
              }
            }, $form.getFieldDecorator('paramUnitId', {
              initialValue: record.paramUnitId,
              rules: [{
                required: !readOnly && paramType === 'unit',
                message: intl.get('cpaas.common.validation.notNull', {
                  name: intl.get('cpfm.individual.common.paramUnit').d('所属单元')
                })
              }]
            })( /*#__PURE__*/React.createElement(Select, {
              style: {
                width: '100%'
              },
              placeholder: intl.get('cpfm.individual.common.paramUnit').d('所属单元'),
              optionLabelProp: "title"
            }, unitList.map(function (unit) {
              return /*#__PURE__*/React.createElement(Option, {
                value: unit.unitId,
                title: unit.unitName,
                disabled: unit.unitType === 'GRID' && unit.unitId !== id
              }, /*#__PURE__*/React.createElement("div", {
                className: "option-title"
              }, unit.unitName), /*#__PURE__*/React.createElement("div", {
                className: "option-value"
              }, unit.unitCode));
            })))), /*#__PURE__*/React.createElement(FormItem, {
              style: {
                display: paramType !== 'unit' ? 'inline-block' : 'none'
              }
            }, $form.getFieldDecorator('paramValue', {
              initialValue: record.paramValue
            })(_this2.getParamValueByType(paramType, $form))), /*#__PURE__*/React.createElement(FormItem, {
              style: {
                display: paramType === 'unit' ? 'inline-block' : 'none'
              }
            }, $form.getFieldDecorator('paramFieldId', {
              initialValue: record.paramFieldId,
              rules: [{
                required: !readOnly && paramType === 'unit',
                message: intl.get('cpaas.common.validation.notNull', {
                  name: intl.get('cpfm.individual.common.paramField').d('字段编码')
                })
              }]
            })( /*#__PURE__*/React.createElement(Select, {
              style: {
                width: '100%'
              },
              optionLabelProp: "title",
              placeholder: intl.get('cpfm.individual.common.paramField').d('字段编码'),
              disabled: readOnly,
              onChange: function onChange(v) {
                return _this2.autoCompleteFieldKey(v, $form);
              }
            }, (fieldList[$form.getFieldValue("paramUnitId")] || []).map(function (f1) {
              return /*#__PURE__*/React.createElement(Option, {
                value: f1.modelFieldId,
                title: f1.unitFieldName
              }, /*#__PURE__*/React.createElement("div", {
                className: "option-title"
              }, f1.unitFieldName), /*#__PURE__*/React.createElement("div", {
                className: "option-value"
              }, f1.unitFieldCode));
            })))));
          }

          return val;
        }
      }, !readOnly && {
        title: intl.get('cpfm.customize.common.op').d('操作'),
        dataIndex: 'op',
        width: 60,
        render: function render(val, record) {
          return /*#__PURE__*/React.createElement(Popconfirm, {
            title: intl.get('cpaas.common.message.confirm.delete').d('是否删除此条记录？'),
            okText: intl.get('cpaas.common.status.yes').d('是'),
            cancelText: intl.get('cpaas.common.status.no').d('否'),
            onConfirm: function onConfirm() {
              return _this2.onDelete(record._status === 'create' ? record._id : record.paramId);
            }
          }, /*#__PURE__*/React.createElement(Icon, {
            type: "delete",
            style: {
              cursor: 'pointer'
            }
          }));
        }
      }].filter(Boolean);
      return /*#__PURE__*/React.createElement(Modal, {
        destroyOnClose: true,
        maskClosable: true,
        width: 1007,
        visible: visible,
        onCancel: onClose,
        onOk: this.onOk,
        bodyStyle: {
          padding: '12px'
        },
        className: styles['params-modal-container'],
        title: intl.get('cpfm.individual.view.message.title.valueParamsConfig').d('值集参数配置')
      }, /*#__PURE__*/React.createElement("header", {
        className: "header"
      }, /*#__PURE__*/React.createElement("div", {
        className: styles.title
      }, intl.get('cpaas.common.button.paramList').d('参数列表')), /*#__PURE__*/React.createElement("div", {
        onClick: this.addParam,
        style: {
          display: readOnly ? 'none' : 'flex',
          lineHeight: '18px',
          height: '28px',
          alignItems: 'center',
          marginLeft: '8px',
          cursor: 'pointer'
        }
      }, /*#__PURE__*/React.createElement(Icon, {
        type: "plus-circle-o",
        style: {
          fontSize: '18px',
          color: '#34a6f8',
          marginRight: '8px'
        }
      }), intl.get('cpaas.common.button.addParam').d('添加参数'))), /*#__PURE__*/React.createElement("div", {
        className: "params-list-area"
      }, /*#__PURE__*/React.createElement(EditTable, {
        bordered: true,
        columns: columns,
        dataSource: paramList,
        pagination: false
      })));
    }
  }]);

  return ConditionModal;
}(React.Component), (_applyDecoratedDescriptor(_class.prototype, "addParam", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "addParam"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "onOk", [_dec2], Object.getOwnPropertyDescriptor(_class.prototype, "onOk"), _class.prototype)), _class));
export { ConditionModal as default };